# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

def send_email(sender_email, sender_password, receiver_email, subject, body):
    # 创建一个MIMEMultipart对象
    message = MIMEMultipart()
    message['From'] = sender_email
    message['To'] = receiver_email
    message['Subject'] = subject

    # 将邮件正文添加到消息中
    message.attach(MIMEText(body, 'plain'))

    try:
        # 连接SMTP服务器（如 Gmail 的 SMTP 服务器）
        server = smtplib.SMTP('smtp.qq.com', 465)  # 使用相应的SMTP地址和端口
        server.starttls()  # 启用TLS加密
        server.login(sender_email, sender_password)  # 登录
        server.send_message(message)  # 发送邮件
        print("邮件发送成功")
    except Exception as e:
        print(f"邮件发送失败: {e}")
    finally:
        server.quit()  # 断开连接

# 使用示例
if __name__ == '__main__':
	send_email(
		sender_email='your_email@example.com',
		sender_password='your_password',
		receiver_email='recipient@example.com',
		subject='测试邮件',
		body='这是一封测试邮件。'
	)
